package com.atguigu.springmvc.crud.handler;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.atguigu.springmvc.crud.dao.DepartmentDao;
import com.atguigu.springmvc.crud.dao.EmployeeDao;
import com.atguigu.springmvc.crud.entities.Employee;

public class EmployeeHandler {

	public EmployeeHandler() {
		
	}

	@InitBinder
	public void initBinder(WebDataBinder binder){
		binder.setDisallowedFields("lastName");
	}
	
	@Autowired
	private EmployeeDao employeeDao;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	@RequestMapping(value="/emp",method=RequestMethod.POST)
	public String save(@Validated Employee employee,BindingResult result,Map<String,Object> map){
		System.out.println("save:"+employee);
		
		if (result.getErrorCount()>0) {
			System.out.println("出错了！");
			for (FieldError error:result.getFieldErrors()) {
				System.out.println(error.getField()+":"+error.getDefaultMessage());
			}
		}
		// employeeDao.save(employee);
		return "redirect:/emps";
	}
	
	public String input(@PathVariable("id") Integer id,Map<String,Object> map){
		//map.put("employee",employeeDao.get(id));
		
		return "input";
	}
	
	
}


































